home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / smailsrc.zip / SMAIL.ZIP / MISC.C < prev    next >
Text File  |  1990-05-05  |  7KB  |  356 lines

  1.  
  2. /*
  3. **  Miscellaneous support functions for smail/rmail
  4. */
  5.  
  6. /*
  7. **    Patched for MS-DOS compatibility by Stephen Trier March, April and
  8. **    May, 1990.  This file is in the public domain.
  9. */
  10.  
  11. #ifndef lint
  12. static char     *sccsid="@(#)misc.c    2.5 (smail) 9/15/87";
  13. #endif
  14.  
  15. # include    <stdio.h>
  16. # include    <sys/types.h>
  17. # include    <ctype.h>
  18. # include    "defs.h"
  19. #ifdef BSD
  20. # include    <sys/time.h>
  21. # include    <sys/timeb.h>
  22. #else
  23. # include    <time.h>
  24. # ifndef MSDOS
  25. #  include       <sys/utsname.h>
  26. # endif /* MSDOS defined */
  27. #endif /* BSD defined */
  28.  
  29. extern int  exitstat;        /* set if a forked mailer fails */
  30. extern enum edebug debug;    /* how verbose we are         */ 
  31. extern enum ehandle handle;    /* what we handle        */
  32. extern char *uuxargs;        /* arguments given to uux       */
  33. extern int  queuecost;        /* threshold for queueing mail  */
  34. extern int  maxnoqueue;        /* max number of uucico's       */
  35. extern enum erouting routing;    /* when to route addresses    */
  36. extern char hostdomain[];    /* */
  37. extern char hostname[];        /* */
  38. extern char hostuucp[];        /* */
  39. extern char *pathfile;        /* location of path database    */
  40. extern char *spoolfile;        /* file name of spooled message */
  41. extern FILE *spoolfp;        /* file ptr  to spooled message */
  42. extern int spoolmaster;        /* set if creator of spoolfile  */
  43.  
  44. extern struct tm *localtime();
  45.  
  46. struct tm *gmt, *loc;        /* GMT and local time structure    */
  47. time_t now;            /* current system time        */
  48. char nows[50];            /* time in ctime format        */
  49. char arpanows[50];        /* time in arpa format        */
  50.  
  51. # ifdef LOG
  52. void
  53. log(command, from, size)
  54. char *command, *from;
  55. long size;
  56. {
  57.     FILE *fd;
  58.     char *logtime, tbuf[50];
  59.     int cmask;
  60.  
  61. #ifdef MSDOS
  62.     if (LOG != NULL)  {
  63. #endif
  64.     logtime = strcpy(tbuf, nows);
  65.     logtime[16] = '\0';
  66.     logtime += 4;
  67.  
  68.     cmask = umask(0);
  69.     fd = fopen(LOG, "a");
  70.     (void) umask(cmask);
  71.  
  72.     if (fd != NULL) {
  73.         (void) fprintf(fd, "%s\t%ld\t%s\t%s\n",
  74.             logtime, size, from, command);
  75.         (void) fclose(fd);
  76.     }
  77. #ifdef MSDOS
  78.     }
  79. #endif
  80. }
  81. # endif
  82.  
  83. # ifdef RECORD
  84. FILE *
  85. record(command, from, size)
  86. char *command, *from;
  87. long size;
  88. {
  89.     FILE *fd;
  90.     char *logtime, buf[SMLBUF];
  91.     int cmask;
  92.  
  93.     logtime = strcpy(buf, nows);
  94.     logtime[16] = 0;
  95.     logtime += 4;
  96.  
  97.     cmask = umask(0);
  98.     fd = fopen(RECORD, "a");
  99.     (void) umask(cmask);
  100.  
  101.     if (fd != NULL) {
  102.         (void) fprintf(fd, "%s: %s, from %s, %ld bytes\n", 
  103.             logtime, command, from, size);
  104.     }
  105.     while(fgets(buf, sizeof(buf), spoolfp) != NULL) {
  106.         (void) fputs(buf, fd);
  107.     }
  108.     (void) fclose(fd);
  109. }
  110. # endif
  111.  
  112.  
  113. setdates()
  114. {
  115.     time_t time();
  116.     struct tm *gmtime();
  117.     char *ctime(), *arpadate();
  118.  
  119.     (void) time(&now);
  120.     (void) strcpy(nows, ctime(&now));
  121.     gmt = gmtime(&now);
  122.     loc = localtime(&now);
  123.     (void) strcpy(arpanows, arpadate(nows));
  124. }
  125.  
  126. /*
  127. **  Note: This routine was taken from sendmail
  128. **
  129. **  ARPADATE -- Create date in ARPANET format
  130. **
  131. **    Parameters:
  132. **        ud -- unix style date string.  if NULL, one is created.
  133. **
  134. **    Returns:
  135. **        pointer to an ARPANET date field
  136. **
  137. **    Side Effects:
  138. **        none
  139. **
  140. **    WARNING:
  141. **        date is stored in a local buffer -- subsequent
  142. **        calls will overwrite.
  143. **
  144. **    Bugs:
  145. **        Timezone is computed from local time, rather than
  146. **        from whereever (and whenever) the message was sent.
  147. **        To do better is very hard.
  148. **
  149. **        Some sites are now inserting the timezone into the
  150. **        local date.  This routine should figure out what
  151. **        the format is and work appropriately.
  152. */
  153.  
  154. char *
  155. arpadate(ud)
  156.     register char *ud;
  157. {
  158.     register char *p;
  159.     register char *q;
  160.     static char b[40];
  161.     extern char *ctime();
  162.     register int i;
  163. #ifndef BSD
  164.     extern char *tzname[];
  165.     time_t t, time();
  166. #else
  167.     /* V7 and 4BSD */
  168.     struct timeb t;
  169.     extern struct timeb *ftime();
  170.     extern char *timezone();
  171. #endif
  172.  
  173.     /*
  174.     **  Get current time.
  175.     **    This will be used if a null argument is passed and
  176.     **    to resolve the timezone.
  177.     */
  178.  
  179. #ifndef BSD
  180.     (void) time(&t);
  181.     if (ud == NULL)
  182.         ud = ctime(&t);
  183. #else
  184.     /* V7 or 4BSD */
  185.     ftime(&t);
  186.     if (ud == NULL)
  187.         ud = ctime(&t.time);
  188. #endif
  189.  
  190.     /*
  191.     **  Crack the UNIX date line in a singularly unoriginal way.
  192.     */
  193.  
  194.     q = b;
  195.  
  196.     p = &ud[8];        /* 16 */
  197.     if (*p == ' ')
  198.         p++;
  199.     else
  200.         *q++ = *p++;
  201.     *q++ = *p++;
  202.     *q++ = ' ';
  203.  
  204.     p = &ud[4];        /* Sep */
  205.     *q++ = *p++;
  206.     *q++ = *p++;
  207.     *q++ = *p++;
  208.     *q++ = ' ';
  209.  
  210.     p = &ud[22];        /* 1979 */
  211.     *q++ = *p++;
  212.     *q++ = *p++;
  213.     *q++ = ' ';
  214.  
  215.     p = &ud[11];        /* 01:03:52 */
  216.     for (i = 8; i > 0; i--)
  217.         *q++ = *p++;
  218.  
  219.                 /* -PST or -PDT */
  220. #ifndef BSD
  221.     p = tzname[localtime(&t)->tm_isdst];
  222. #else
  223.     p = timezone(t.timezone, localtime(&t.time)->tm_isdst);
  224. #endif
  225.     if (p[3] != '\0')
  226.     {
  227.         /* hours from GMT */
  228.         p += 3;
  229.         *q++ = *p++;
  230.         if (p[1] == ':')
  231.             *q++ = '0';
  232.         else
  233.             *q++ = *p++;
  234.         *q++ = *p++;
  235.         p++;        /* skip ``:'' */
  236.         *q++ = *p++;
  237.         *q++ = *p++;
  238.     }
  239.     else
  240.     {
  241.         *q++ = ' ';
  242.         *q++ = *p++;
  243.         *q++ = *p++;
  244.         *q++ = *p++;
  245.     }
  246.  
  247.     p = &ud[0];        /* Mon */
  248.     *q++ = ' ';
  249.     *q++ = '(';
  250.     *q++ = *p++;
  251.     *q++ = *p++;
  252.     *q++ = *p++;
  253.     *q++ = ')';
  254.  
  255.     *q = '\0';
  256.     return (b);
  257. }
  258.  
  259. /*
  260.  *    The user name "postmaster" must be accepted regardless of what
  261.  *    combination of upper and lower case is used.  This function is
  262.  *    used to convert all case variants of "postmaster" to all lower
  263.  *    case.  If the user name passed in is not "postmaster", it is
  264.  *    returned unchanged.
  265.  */
  266. char *
  267. postmaster(user)
  268. char *user;
  269. {
  270.     static char *pm = "postmaster";
  271.  
  272.     if(strcmpic(user, pm) == 0) {
  273.         return(pm);
  274.     } else {
  275.         return(user);
  276.     }
  277. }
  278.  
  279. /*
  280.  * Return 1 iff the string is "UUCP" (ignore case).
  281.  */
  282. isuucp(str)
  283. char *str;
  284. {
  285.     if(strcmpic(str, "UUCP") == 0) {
  286.         return(1);
  287.     } else {
  288.         return(0);
  289.     }
  290. }
  291.  
  292. /*
  293. ** sform(form) returns a pointer to a string that tells what 'form' means
  294. */
  295.  
  296. char *
  297. sform(form)
  298. enum eform form;
  299. {
  300.     if(form == ERROR)  return("ERROR");
  301.     if(form == LOCAL)  return("LOCAL");
  302.     if(form == DOMAIN) return("DOMAIN");
  303.     if(form == UUCP)   return("UUCP");
  304.     if(form == ROUTE)  return("ROUTE");
  305.     return("UNKNOWN");
  306. }
  307.  
  308. /*
  309. **
  310. **  getmynames(): what is my host name and host domain?
  311. **
  312. **  Hostname set by -h, failing that by #define HOSTNAME, failing
  313. **  that by gethostname() or uname().
  314. **  
  315. **  Hostdomain set by -h, failing that by #define HOSTDOMAIN,
  316. **  failing that as hostname.MYDOM, or as just hostname.
  317. **
  318. **  See defs.h for the inside story.
  319. **
  320. */
  321.  
  322. getmynames()
  323. {
  324. #ifdef HOSTNAME
  325.     if (!*hostname)
  326.         (void) strcpy(hostname, HOSTNAME);
  327. #endif
  328. #ifdef GETHOSTNAME
  329.     if (!*hostname)
  330.         gethostname(hostname, SMLBUF - 1);
  331. #endif
  332. #ifdef UNAME
  333.     if (!*hostname) {
  334.         struct utsname site;
  335.  
  336.         if (uname(&site) < 0)
  337.             error(EX_SOFTWARE, "uname() call failed", 0);
  338.         (void) strcpy(hostname, site.nodename);
  339.     }
  340. #endif
  341.     if (!*hostname)
  342.         error(EX_SOFTWARE, "can't determine hostname.\n", 0);
  343. #ifdef HOSTDOMAIN
  344.     if (!*hostdomain)
  345.         (void) strcpy(hostdomain, HOSTDOMAIN);
  346. #endif
  347. #ifdef MYDOM
  348.     if (!*hostdomain)
  349.         (void) strcat(strcpy(hostdomain, hostname), MYDOM);
  350. #endif
  351.     if (!*hostdomain)
  352.         (void) strcpy(hostdomain, hostname);
  353.  
  354.     (void) strcat(strcpy(hostuucp, hostname), ".UUCP");
  355. }
  356.